﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Web.Utility;
using System.Data;
using BySoft.Server.Common.Enum;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common;
using OneSoft.EntityCore;
using BySoft.Server.Consts;
using BySoft.Server.Common.Schema;
using System.IO;
using System.Configuration;
using BySoft.Web.Controls;
using System.Threading;
using BySoft.Web.Site;

namespace ZCZP.Web.Site.OutSource
{
    public partial class OutOrderView : AuthBasePage
    {
        int m_count = 0;
        private static int iPageIndex = 0;

        #region 变量定义
        protected DataTable BillDT
        {
            get
            {
                if (Session["BillDT"] != null)
                {
                    return (DataTable)Session["BillDT"];
                }
                else
                {
                    return CreateDT();
                }
            }
            set
            {
                Session["BillDT"] = value;
            }
        }
        protected DataTable BillDetailDT
        {
            get
            {
                if (Session["BillDetailDT"] != null)
                {
                    return (DataTable)Session["BillDetailDT"];
                }
                else
                {
                    return CreateDetailDT();
                }
            }
            set
            {
                Session["BillDetailDT"] = value;
            }
        }

        private DataTable CreateDT()
        {
            return BsOutorder.CreateDT();
        }
        private DataTable CreateDetailDT()
        {
            return BsOutorder.CreateDetailDT();
        }

        #endregion

        #region 初始化及绑定数据
        void InitData()
        {
            lbBillID.Text = UIHelper.GetQueryStringValue("id");
            InitDetailData();
          
        }
        void InitDetailData()
        {
            try
            {
                DataTable dtTemp = CreateDetailDT();

                DataTable dt = BsOutorder.SelectHeadData(lbBillID.Text);

                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr0 = dt.Rows[0];
                    cpBillDate.Text = dr0["BillDate"].ToString().Trim();
                    hidSupplierID.Value = dr0["SupplierID"].ToString().Trim();
                    txtSupplierName.Text = dr0["SupplierName"].ToString().Trim();
                    hidOutProcessID.Value = dr0["OutProcessID"].ToString().Trim();
                    txtOutProcessName.Text = dr0["OutProcessName"].ToString().Trim();
                    txtWorkOrderNo.Text = dr0["WorkOrderNo"].ToString().Trim();
                    txtScheduleID.Text = dr0["ScheduleID"].ToString().Trim();
                    txtBackMaterialID.Text = dr0["BackMaterialID"].ToString().Trim();
                    txtBackMaterialName.Text = dr0["BackMaterialName"].ToString().Trim();
                    decimal dOutNums = CommonMethod.ConvertToDecimal(dr0["BackNums"]);
                    txtBackNums.Text = dOutNums.ToString();
                    txtProcessCost.Text = dr0["ProcessCost"].ToString().Trim();


                    txtOutProcessID.Text = CommonMethod.GetString(dr0["OutProcessID"]);
                    txtBackProcessID.Text = CommonMethod.GetString(dr0["BackProcessID"]);
                    //hdOutOrderNum.Value = CommonMethod.GetString(dr0["OutOrderNum"]);
                    //hdBackOrderNum.Value = CommonMethod.GetString(dr0["BackOrderNum"]);
                    hdScheduleDetailID.Value = CommonMethod.GetString(dr0["ScheduleDetailID"]);


                }
                DataTable dtDetail = BsOutorder.SelectDetailData(lbBillID.Text);
                if (dtDetail != null && dtDetail.Rows.Count > 0)
                {

                    grdResult.DataSource = dtDetail;
                    grdResult.DataBind();
                }
                BillDT = dt.Copy();
                BillDetailDT = dtDetail.Copy();
            }
            catch (Exception ex) { UIHelper.AlertMessage(this, ex.Message.Trim()); }
        }
        private void BindData(int pageIndex)
        {
            string strProcessID = CommonMethod.GetString(hidOutProcessID.Value);
            string strScheduleDetailID = CommonMethod.GetString(hdScheduleDetailID.Value);
            string strMaterialID = txtBackMaterialID.Text.Trim();

            if (string.IsNullOrEmpty(strProcessID))
            {
                return;
            }


            DataTable dtProcessDetail =
                        BsOutorder.SelectOutProcessDetail(strScheduleDetailID, strProcessID);
            //dtProcessDetail.Columns.Add("IsNew");//是否新增
            //dtProcessDetail.Columns.Add("IsDel");//是否删除

            BillDetailDT = dtProcessDetail.Copy();

            if (dtProcessDetail.Rows.Count > 0)
            {
                //header
                DataRow dr0 = dtProcessDetail.Rows[0];
                txtScheduleID.Text = CommonMethod.GetString(dr0["ScheduleID"]);
                txtWorkOrderNo.Text = CommonMethod.GetString(dr0["WorkOrderNo"]);
                txtBackNums.Text = CommonMethod.GetString(dr0["BackNums"]);
                txtOutProcessName.Text = CommonMethod.GetString(dr0["ProcessName"]);
                txtOutProcessID.Text = CommonMethod.GetString(dr0["ProcessID"]);
                txtBackProcessID.Text = CommonMethod.GetString(dr0["BackProcessID"]);

               
                hdScheduleDetailID.Value = CommonMethod.GetString(dr0["ScheduleDetailID"]);

                //需要从选取的数据中复制到detail栏位名称
                string[] fldList = new string[] { 
                   "MaterialTypeName","MaterialID","MaterialName","Specifications","Unit","Nums"
                };
                DataTable dtDetail = BsOutorder.SelectDetailData("-1");//生个空的detail

                for (int i = 0; i < dtProcessDetail.Rows.Count; i++)
                {
                    DataRow drProcessDetail = dtProcessDetail.Rows[i];

                    DataRow dr = dtDetail.Rows.Add();
                    CopyDataRowProperty(drProcessDetail, dr, fldList);
                    dr["BillID"] = GetString(lbBillID);
                    dr["DetailID"] = (i + 1).ToString();
                    dr["IsNew"] = "1";
                    dr["IsDel"] = "-1";
                }


                BillDetailDT = dtDetail;

                grdResult.DataSource = dtDetail;

                grdResult.DataBind();
                SetControlAttr(new MdCustomerdetailEntity(), grdResult);

            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, CreateDetailDT());
            }

            hidOutProcessID.Value = strProcessID;

            UIHelper.RunScript(this, "iframeAutoFit();");
        }

        private void CopyDataRowProperty(DataRow drSource, DataRow drTarget, string[] fldList)
        {
            foreach (string name in fldList)
            {
                drTarget[name] = drSource[name];
            }

        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                iPageIndex = 0;
                Session["BillDT"] = null;
                Session["DetailDT"] = null;
                InitData();

            }
            Master.ValidationGroup = "gSubmit";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Tabs", "iframeAutoFit();", true);
        }
        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            iPageIndex = e.PageIndex;
            BindData(iPageIndex);
        }  

        #endregion 
    


        #region Grid事件
        /// <summary>
        ///   行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          
        }
        #endregion

        #region 本地函数

        private bool ValidateData()
        {
            return true;
        }


        #endregion
    }
}
